<template>
  <div class="file-info-container">
    <div class="header">
      <h2>文件信息</h2>
      <el-button @click="$router.push('/')" plain>返回首页</el-button>
    </div>
    
    <div class="content">
      <el-card shadow="hover" v-loading="loading">
        <template v-if="fileInfo">
          <div class="file-detail">
            <div class="file-header">
              <div class="file-icon">
                <el-icon><Document /></el-icon>
              </div>
              <h3>{{ fileInfo.originalFileName }}</h3>
            </div>
            
            <div class="detail-grid">
              <div class="detail-item">
                <div class="detail-label">文件大小</div>
                <div class="detail-value">{{ formatFileSize(fileInfo.fileSize) }}</div>
              </div>
              
              <div class="detail-item">
                <div class="detail-label">文件类型</div>
                <div class="detail-value file-type">{{ fileInfo.fileType }}</div>
              </div>
              
              <div class="detail-item">
                <div class="detail-label">上传时间</div>
                <div class="detail-value">{{ formatDateTime(fileInfo.uploadTime) }}</div>
              </div>
              
              <div class="detail-item">
                <div class="detail-label">过期时间</div>
                <div class="detail-value">{{ formatDateTime(fileInfo.expirationTime) }}</div>
              </div>
              
              <div class="detail-item">
                <div class="detail-label">下载次数</div>
                <div class="detail-value">{{ fileInfo.downloadCount }}</div>
              </div>
              
              <div class="detail-item" v-if="fileInfo.description">
                <div class="detail-label">文件描述</div>
                <div class="detail-value">{{ fileInfo.description }}</div>
              </div>
            </div>
            
            <div class="download-area">
              <el-button
                type="primary"
                size="large"
                :loading="downloading"
                @click="handleDownload"
                class="download-btn"
              >
                <el-icon><Download /></el-icon> 下载文件
              </el-button>
            </div>
          </div>
        </template>
        
        <template v-else-if="error">
          <el-result
            icon="error"
            :title="error"
            sub-title="文件可能已过期或下载码无效"
          >
            <template #extra>
              <el-button type="primary" @click="$router.push('/')">返回首页</el-button>
            </template>
          </el-result>
        </template>
      </el-card>
    </div>
  </div>
</template>

<script>
import { ref, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import { Download, Document } from '@element-plus/icons-vue'
import { formatFileSize, formatDateTime } from '../utils/format'
import api from '../utils/api'

export default {
  name: 'FileInfoPage',
  components: {
    Download,
    Document
  },
  setup() {
    const route = useRoute()
    const downloadCode = route.params.code
    
    const fileInfo = ref(null)
    const loading = ref(true)
    const error = ref('')
    const downloading = ref(false)
    
    onMounted(async () => {
      await fetchFileInfo()
    })
    
    const fetchFileInfo = async () => {
      loading.value = true
      error.value = ''
      
      try {
        const response = await api.getFileByCode(downloadCode)
        const { code, data, message } = response.data
        
        if (code === 200 && data) {
          // 确保数据字段都有合法值
          fileInfo.value = {
            ...data,
            fileSize: data.fileSize || 0,
            fileType: data.fileType || 'application/octet-stream',
            uploadTime: data.uploadTime || '',
            expirationTime: data.expirationTime || '',
            downloadCount: data.downloadCount || 0
          }
        } else {
          error.value = message || '获取文件信息失败'
        }
      } catch (err) {
        console.error('获取文件信息错误', err)
        error.value = '获取文件信息失败，请检查下载码是否正确'
      } finally {
        loading.value = false
      }
    }
    
    const handleDownload = () => {
      if (!fileInfo.value) return
      
      downloading.value = true
      
      // 创建下载链接
      const downloadUrl = `/api/files/download/${downloadCode}`
      
      // 创建隐藏的a标签实现下载
      const link = document.createElement('a')
      link.href = downloadUrl
      link.download = fileInfo.value.originalFileName
      document.body.appendChild(link)
      link.click()
      document.body.removeChild(link)
      
      setTimeout(() => {
        downloading.value = false
        ElMessage.success('文件下载已开始')
      }, 1000)
    }
    
    return {
      fileInfo,
      loading,
      error,
      downloading,
      formatFileSize,
      formatDateTime,
      handleDownload
    }
  }
}
</script>

<style scoped>
.file-info-container {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.header h2 {
  font-size: 1.8rem;
  font-weight: 700;
  color: var(--primary-color);
  margin: 0;
  letter-spacing: -0.5px;
}

.content {
  margin-bottom: 20px;
}

.file-detail {
  padding: 10px;
}

.file-header {
  display: flex;
  align-items: center;
  gap: 15px;
  margin-bottom: 30px;
}

.file-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 60px;
  height: 60px;
  background-color: var(--primary-light);
  border-radius: 12px;
  color: var(--primary-dark);
}

.file-icon i {
  font-size: 30px;
}

.file-header h3 {
  font-size: 1.5rem;
  font-weight: 600;
  color: var(--text-color);
  word-break: break-all;
  margin: 0;
}

.detail-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 20px;
  margin-bottom: 30px;
}

.detail-item {
  padding: 15px;
  background-color: var(--background-color);
  border-radius: 12px;
  transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.detail-item:hover {
  transform: translateY(-2px);
  box-shadow: var(--card-shadow);
}

.detail-label {
  font-size: 0.9rem;
  font-weight: 500;
  color: var(--text-secondary);
  margin-bottom: 5px;
}

.detail-value {
  font-size: 1.1rem;
  font-weight: 500;
  color: var(--text-color);
  word-break: break-all;
}

.file-type {
  font-family: monospace;
}

.download-area {
  text-align: center;
  margin-top: 30px;
}

.download-btn {
  padding: 12px 30px;
  font-size: 1.1rem;
  border-radius: 12px;
}

@media (max-width: 768px) {
  .detail-grid {
    grid-template-columns: 1fr;
    gap: 15px;
  }
  
  .file-header {
    flex-direction: column;
    text-align: center;
  }
  
  .file-header h3 {
    font-size: 1.3rem;
  }
}

@media (max-width: 480px) {
  .file-info-container {
    padding: 15px;
  }
  
  .header h2 {
    font-size: 1.5rem;
  }
  
  .detail-item {
    padding: 12px;
  }
  
  .download-btn {
    width: 100%;
  }
}
</style> 